﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    public class CivilDataProcessAnalysisDAL
    {
        private string DbName;
        public CivilDataProcessAnalysisDAL()
        {
            DbName = "CaseWorkingall";
        }

        /// <summary>
        /// 根据案由获取本院认为或者审理查明内容
        /// </summary>
        /// <param name="code">字典类型编码</param>
        /// <returns></returns>
        public DataTable GetDataByType(string type, string ccID,int num)
        {
            string sql = string.Empty;
            if (type == "本院认为")
            {
                sql = string.Format(@" SELECT top {1} c.ID as caseId,c.CourtAffirm as description FROM [dbo].[CS_Case] a,CS_CaseCauseRelation b,CaseLogicPartInfoes c 
WHERE a.CaseID=b.CaseID  AND b.CaseID=c.ID 
AND a.CaseTypeID = 'CT1508061134410046' AND a.DocStyleID = 't1' AND b.CaseCauseID='{0}' and ProcessingStepId>=15 and (a.IsComSuit is null or a.IsComSuit='') order by a.ProcessingTime desc ", ccID,num);
            }
            else if (type == "审理查明")
            {
                sql = string.Format(@" SELECT top {1} c.caseid as caseId,c.c_text as description FROM [dbo].[CS_Case] a,CS_CaseCauseRelation b,CaseLogicPartInfoSon c 
WHERE a.CaseID=b.CaseID  AND b.CaseID=c.caseid 
AND a.CaseTypeID = 'CT1508061134410046' AND a.DocStyleID = 't1' AND b.CaseCauseID='{0}' and ProcessingStepId>=15 AND c.c_type='Judge' and (a.IsComSuit is null or a.IsComSuit='') order by a.ProcessingTime desc ", ccID,num);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            oCmd.CommandTimeout = 3000;
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 根据caseId获取人名等信息做挖空处理
        /// </summary>
        public DataTable GetDataByCaseId(string caseId)
        {
            string sql = string.Format(" select SP_Name from dbo.CS_SuitParticipant where  caseid = '{0}' ", caseId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            oCmd.CommandTimeout = 3000;
            var otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            return new DataTable();
        }

        /// <summary>
        /// 修改case表状态
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateCaseIsComSuit(string caseId)
        {
            string sql = string.Empty;
            sql = string.Format(@" Update CS_Case set IsComSuit = 1 where CaseId ='{0}' ", caseId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            oCmd.CommandTimeout = 3000;
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
    }
}
